﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server.Diagnostics;
using Microsoft.SharePoint.Administration;
using System.Reflection;
using Microsoft.SharePoint.Utilities;

namespace TVR.Timerjobs.Utilitys
{
    public class Utility
    {
        /// <summary>
        /// Writes to SP log.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="message">The message.</param>
        public static void WriteToSPLog(string sender, string message)
        {
            PortalLog.LogString(String.Concat(sender, " --> ", message));
        }

        /// <summary>
        /// Removes the timer job.
        /// </summary>
        /// <param name="jobTitle">The job title.</param>
        /// <param name="webApplication">The web application.</param>
        public static void RemoveTimerJob(string jobTitle, SPWebApplication webApplication)
        {
            List<Guid> jobs = new List<Guid>();

            foreach (SPJobDefinition jd in webApplication.JobDefinitions)
            {
                if (jd.Title == jobTitle)
                    jobs.Add(jd.Id);
            }

            foreach (Guid g in jobs)
                webApplication.JobDefinitions[g].Delete();
        }

        /// <summary>
        /// Method to populate a list with all the class
        /// in the namespace provided by the user
        /// </summary>
        /// <param name="nameSpace">The namespace the user wants searched</param>
        /// <returns></returns>
        public static List<string> GetAllSPJobDefenitionClasses(string nameSpace)
        {
            //create an Assembly and use its GetExecutingAssembly Method
            //http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx
            Assembly asm = Assembly.GetExecutingAssembly();
            //create a list for the namespaces
            List<string> namespaceList = new List<string>();
            //create a list that will hold all the classes
            //the suplied namespace is executing
            List<string> returnList = new List<string>();
            //loop through all the "Types" in the Assembly using
            //the GetType method:
            //http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx
            foreach (Type type in asm.GetTypes())
            {
                if (type.Namespace == nameSpace && type.BaseType == typeof(SPJobDefinition))
                    namespaceList.Add(type.Name);
            }
            //now loop through all the classes returned above and add
            //them to our classesName list
            foreach (String className in namespaceList)
                returnList.Add(className);
            //return the list
            return returnList;
        }

        /// <summary>
        /// Checks if the timer job name already exists.
        /// </summary>
        /// <param name="jobTitle">The job title.</param>
        /// <param name="webApplication">The web application.</param>
        public static bool TimerjobNameExists(string jobTitle, SPWebApplication webApplication)
        {
            foreach (SPJobDefinition jd in webApplication.JobDefinitions)
            {
                if (jd.Title == jobTitle)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Set a propertyvalue in a SPPRopertybag
        /// </summary>
        /// <param name="pBag"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static SPPropertyBag SetPropertybagKey(SPPropertyBag pBag, string key, string value)
        {
            if (pBag.ContainsKey(key))
            {
                pBag[key] = value;
            }
            else
            {
                pBag.Add(key, value);
            }
            pBag.Update();

            return pBag;
        }
    }
}
